<?php
	require("../wp-blog-header.php");
	include("event-config.php");

	if (!isset($_GET['p'])) {
		header("Location: error.php");
	}
	else{
		$event_id = $_GET['p'];
		//get time
		$query = "SELECT
		DATE_FORMAT(`start_date`,'%d-%m-%Y-%H-%i') AS `start_time`, 
		DATE_FORMAT(`end_date`,'%d-%m-%Y-%H-%i') AS `end_time`
    	FROM $EVENT WHERE `event_id` = '$event_id'";
		$result 	= first_row($query);
		$start_time = $result["start_time"];
		$end_time 	= $result["end_time"];
		$now 		= date("d-m-Y-H-i");
		$eventStat 	= "ok";
		if (($start_time != "00-00-0000-00-00" && compare_date($now, $start_time) == 2)
			|| ($end_time != "00-00-0000-00-00" && compare_date($now, $end_time) == 1)) {
			$eventStat = "not_ok";
		}

		if ($eventStat == "not_ok") {
			header("Location: error.php");
		}
		else{
			//Cancel register
			$user_login = wp_get_current_user()->user_login;
			$query1 = "DELETE FROM `event_poll_result` 
					  WHERE `user_login` = '$user_login' 
					  	AND `answer_id` 
					  		 IN (SELECT `answer_id` FROM `event_poll_answer` WHERE `event_id` = '$event_id')";
			$query2 = "DELETE FROM $EVENT_JOIN WHERE `event_id` = '$event_id' AND `user_login` = '$user_login'";
			
			if(execute($query1) && execute($query2)){
				header("Location: event.php?p=$event_id");
			}
			else{
				die("Loi thuc hien lenh");
			}	
		}
	}
?>